package com.ruoyi.category.service.impl;

import java.util.List;

import com.ruoyi.article.domain.Article;
import com.ruoyi.article.domain.vo.ArticleVO;
import com.ruoyi.article.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.category.mapper.ArticleCategoryMapper;
import com.ruoyi.category.domain.ArticleCategory;
import com.ruoyi.category.service.IArticleCategoryService;

/**
 * 文章分类Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-22
 */
@Service
public class ArticleCategoryServiceImpl implements IArticleCategoryService
{
    @Autowired
    private ArticleCategoryMapper articleCategoryMapper;
    @Autowired
    private IArticleService articleService;

    /**
     * 查询文章分类
     *
     * @param categoryId 文章分类主键
     * @return 文章分类
     */
    @Override
    public ArticleCategory selectArticleCategoryByCategoryId(Long categoryId)
    {
        return articleCategoryMapper.selectArticleCategoryByCategoryId(categoryId);
    }

    /**
     * 查询文章分类列表
     *
     * @param articleCategory 文章分类
     * @return 文章分类
     */
    @Override
    public List<ArticleCategory> selectArticleCategoryList(ArticleCategory articleCategory)
    {
        return articleCategoryMapper.selectArticleCategoryList(articleCategory);
    }

    /**
     * 新增文章分类
     *
     * @param articleCategory 文章分类
     * @return 结果
     */
    @Override
    public int insertArticleCategory(ArticleCategory articleCategory)
    {
        return articleCategoryMapper.insertArticleCategory(articleCategory);
    }

    /**
     * 修改文章分类
     *
     * @param articleCategory 文章分类
     * @return 结果
     */
    @Override
    public int updateArticleCategory(ArticleCategory articleCategory)
    {
        return articleCategoryMapper.updateArticleCategory(articleCategory);
    }

    /**
     * 批量删除文章分类
     *
     * @param categoryIds 需要删除的文章分类主键
     * @return 结果
     */
    @Override
    public int deleteArticleCategoryByCategoryIds(Long[] categoryIds)
    {
        //查询该分类下面是否有文章
        for (Long categoryId : categoryIds){
            List<ArticleVO> articleList = articleService.selectArticleByCategoryId(categoryId);
            if (articleList.size() > 0){
                return 0;
            }
        }
        return articleCategoryMapper.deleteArticleCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除文章分类信息
     *
     * @param categoryId 文章分类主键
     * @return 结果
     */
    @Override
    public int deleteArticleCategoryByCategoryId(Long categoryId)
    {
        return articleCategoryMapper.deleteArticleCategoryByCategoryId(categoryId);
    }
}
